package com.easy.sk.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.SeckillResult;
import com.easy.common.service.ISeckillResultService;
import com.easy.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/seckill/result")
@CrossOrigin
public class SeckillResultController {

    @Autowired
    private ISeckillResultService seckillResultService;

    // 根据id获取秒杀结果信息
    @GetMapping("/{id}")
    public Result getResultById(@PathVariable Integer id) {
        return Result.success(seckillResultService.getResultById(id));
    }

    // 根据用户id获取秒杀结果列表
    @GetMapping("/user/{userId}")
    public Result getResultsByUserId(@PathVariable Integer userId) {
        List<SeckillResult> results = seckillResultService.getResultsByUserId(userId);
        return Result.success(results);
    }

    // 根据秒杀商品id获取秒杀结果列表
    @GetMapping("/seckillgoods/{seckillGoodsId}")
    public Result getResultsBySeckillGoodsId(@PathVariable Integer seckillGoodsId) {
        List<SeckillResult> results = seckillResultService.getResultsBySeckillGoodsId(seckillGoodsId);
        return Result.success(results);
    }

    // 根据活动id获取秒杀结果列表
    @GetMapping("/activity/{activityId}")
    public Result getResultsByActivityId(@PathVariable Integer activityId) {
        List<SeckillResult> results = seckillResultService.getResultsByActivityId(activityId);
        return Result.success(results);
    }

    // 添加秒杀结果
    @PostMapping("/add")
    public Result addResult(@RequestBody SeckillResult result) {
        int res = seckillResultService.addResult(result);
        return res > 0 ? Result.success("添加成功") : Result.fail(500, "添加失败");
    }

    // 更新秒杀结果
    @PutMapping("/update")
    public Result updateResult(@RequestBody SeckillResult result) {
        int res = seckillResultService.updateResult(result);
        return res > 0 ? Result.success("更新成功") : Result.fail(500, "更新失败");
    }

    // 删除秒杀结果（软删除）
    @DeleteMapping("/delete/{id}")
    public Result deleteResult(@PathVariable Integer id) {
        int res = seckillResultService.deleteResult(id);
        return res > 0 ? Result.success("删除成功") : Result.fail(500, "删除失败");
    }

    // 获取秒杀结果列表
    @GetMapping("/list")
    public Result getResultList(SeckillResult result) {
        List<SeckillResult> results = seckillResultService.getAll(result);
        return Result.success(results);
    }

    // 获取秒杀结果分页列表
    @GetMapping("/page")
    public Result getResultPage(SeckillResult result, Integer current, Integer size) {
        IPage<SeckillResult> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size);
        IPage<SeckillResult> resultPage = seckillResultService.getPage(result, page);
        return Result.success(resultPage);
    }
}
